package com.kingint.web.admin.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.bas.entity.BasIdentity;
import com.kingint.mbg.group.Insert;
import com.kingint.mbg.group.Update;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.validateUtil.ValidList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.kingint.common.unitedresult.K;

import com.kingint.web.admin.base.service.BasWorkOrderTypeService;
import com.kingint.mbg.bas.entity.BasWorkOrderType;

import javax.annotation.Resource;
import java.util.List;


/**
 * @描述：工单类型表控制类
 * @作者: wxh
 * @日期: 2020-12-21
 */
@RestController
@RequestMapping("/basWorkOrderType")
@Api(value = "BasWorkOrderTypeController", description = "工单类型表")
@CrossOrigin
@Slf4j
public class BasWorkOrderTypeController{
    @Resource
    private UmsAdminService adminService;

    @Autowired
    public BasWorkOrderTypeService basWorkOrderTypeService;


    @ApiOperation(value = "添加工单类型表")
    @PostMapping("/create")
    //@PreAuthorize("hasAuthority('basWorkOrderType:create')")
    public Object create(@Validated(Insert.class) @RequestBody BasWorkOrderType basWorkOrderType){
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();//对应的酒店id
        if(StringUtils.isEmpty(hotelId)){
            log.debug("当前账号没有关联对应的酒店");
            return K.error();
        }
        Boolean bool=basWorkOrderTypeService.saveOrUpdate(basWorkOrderType);
        if(bool){
            return K.ok().data(basWorkOrderType.getId());
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "批量添加工单类型表")
    @PostMapping("/insertList")
    //  @PreAuthorize("hasAuthority('basWorkOrderType:insertList')")
    public Object create(@RequestBody @Validated(Insert.class)  ValidList<BasWorkOrderType> basWorkOrderType){
        Boolean bool=basWorkOrderTypeService.saveOrUpdateBatch(basWorkOrderType);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "更新工单类型表")
    @PutMapping("/update")
    //@PreAuthorize("hasAuthority('basWorkOrderType:update')")
    public Object update(@Validated(Update.class) @RequestBody BasWorkOrderType basWorkOrderType){
        Boolean bool= basWorkOrderTypeService.updateById(basWorkOrderType);
        if(bool){
            return K.ok().data(basWorkOrderType.getId());
        }else{
            return K.error();
        }
    }


    @ApiOperation(value = "删除工单类型表")
    @DeleteMapping("/delete/{id}")
    //@PreAuthorize("hasAuthority('basWorkOrderType:delete')")
    public Object delete(@PathVariable("id") Integer id){
        BasWorkOrderType basWorkOrderType=new BasWorkOrderType();
        basWorkOrderType.setId(id);
        basWorkOrderType.setStatus(1);
        Boolean bool= basWorkOrderTypeService.updateById(basWorkOrderType);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "根据工单类型表名称分页获取工单类型表列表")
    @GetMapping("/list")
    // @PreAuthorize("hasAuthority('basWorkOrderType:list')")
    public Object getList(BasWorkOrderType basWorkOrderType,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if(StringUtils.isEmpty(hotelId)){
            log.debug("当前账号没有关联对应的酒店");
            return K.error();
        }
        QueryWrapper<BasWorkOrderType> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0);
        String name = basWorkOrderType.getName();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }

        Page<BasWorkOrderType> page = new Page<>(pageNum, pageSize);
        IPage<BasWorkOrderType> info=basWorkOrderTypeService.page(page,wrapper);
        return K.ok().data(info);
    }

    @ApiOperation(value = "根据工单类型表名称分页获取工单类型表列表")
    @GetMapping("/listAll")
    //  @PreAuthorize("hasAuthority('basWorkOrderType:list')")
    public Object getListAll(){
        List<BasWorkOrderType> list = basWorkOrderTypeService.list(null);
//        Page<BasWorkOrderType> page = new Page<>(pageNum, pageSize);
//        IPage<BasWorkOrderType> info=basWorkOrderTypeService.page(page,null);
        return K.ok().data(list);
    }

    @ApiOperation(value = "根据编号查询工单类型表信息")
    @GetMapping("/{id}")
    // @PreAuthorize("hasAuthority('basWorkOrderType:get')")
    public Object getItem(@PathVariable("id") Integer id){
        BasWorkOrderType basWorkOrderType=basWorkOrderTypeService.getById(id);
        return K.ok().data(basWorkOrderType);
    }
}